from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from utils import resolve_object, comment_notify, answer_notify, increment_comment, post_notify
from django.db.models.signals import post_save
from django.contrib.comments.signals import comment_was_posted
from django.dispatch import receiver
from django.contrib.comments import Comment
from userena.signals import activation_complete
from django.utils.translation import ugettext_lazy as _



class Post(models.Model):
    title = models.CharField(max_length=50)
    body = models.TextField(blank=True)
    short_body = models.TextField()
    author = models.ForeignKey(User)
    datetime = models.DateTimeField(auto_now_add=True)

    @models.permalink
    def get_absolute_url(self):
        return ('post_view', [str(self.id)])

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['-datetime']

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'datetime', 'author')
    ordering = ('-datetime',)

admin.site.register(Post, PostAdmin)




class PostMeta(models.Model):
    post = models.OneToOneField(Post)
    comments_count = models.PositiveIntegerField(default=0)

    def __unicode__(self):
        return u'Metas to post "%s"' % self.post

class PostMetaAdmin(admin.ModelAdmin):
    pass

admin.site.register(PostMeta, PostMetaAdmin)

@receiver(post_save, sender=Post, weak=False, dispatch_uid='blog.models.create_post_meta')
def create_post_meta(sender, instance, created, **kwargs):
    if created:
        PostMeta.objects.create(post=instance)
        post_notify(instance)
        PostUserData.objects.create(
                    user = instance.author,
                    post = instance,
                    read = 0
                    )

@receiver(comment_was_posted, weak=False, dispatch_uid='blog.models.comment_was_created')
def comment_was_created(sender, comment, request, **kwargs):
    post = resolve_object(comment)
    increment_comment(post)
    comment_notify(post, comment)
    answer_notify(post, comment)




class PostUserData(models.Model):
    user = models.ForeignKey(User)
    post = models.ForeignKey(Post)
    read = models.PositiveIntegerField(default=0)

    def __unicode__(self):
        return u'"%s" read by %s' % (self.post.title, self.user.username)

class PostUserDataAdmin(admin.ModelAdmin):
    pass

admin.site.register(PostUserData, PostUserDataAdmin)




class UserNotification(models.Model):
    user = models.OneToOneField(User)
    on_my_comments = models.BooleanField(default=False, verbose_name=_('notificate about answers on my comments:'))
    on_new_post = models.BooleanField(default=False, verbose_name=_('notificate about new posts:'))
    posts = models.ManyToManyField(Post, blank=True, verbose_name=_('notificate about comments in selected posts:'))

    def __unicode__(self):
        return '%s notifications' % self.user.username

class UserNotificationAdmin(admin.ModelAdmin):
    filter_horizontal = ('posts',)
    pass

admin.site.register(UserNotification, UserNotificationAdmin)

@receiver(activation_complete, weak=False, dispatch_uid='blog.models.create_user_notification')
def create_user_notification(user, **kwargs):
    UserNotification.objects.create(user=user)

